package org.liaohailong.helloworld.study;

/**
 * Author: liaohailong
 * Time: 2021/5/11 15:36
 * Describe:
 */
public class MyLinkedList {

    private Node head;
    private Node foot;
    private int size;

    /**
     * Initialize your data structure here.
     */
    public MyLinkedList() {

    }

    /**
     * Get the value of the index-th node in the linked list. If the index is invalid, return -1.
     */
    public int get(int index) {
        if (size <= 0) return -1;

        if (index == 0) return head.val;
        else if (index == size - 1) return foot.val;

        Node node;
        int pos;
        int mid = size / 2;
        if (index > mid) {
            // traversal from foot
            node = foot;
            pos = size - 1;
            while (node != null) {
                if (pos == index) return node.val;
                node = node.prev;
                pos--;
            }
        } else {
            // traversal from head
            node = head;
            pos = 0;
            while (node != null) {
                if (pos == index) return node.val;
                node = node.next;
                pos++;
            }
        }

        return -1;
    }

    /**
     * Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list.
     */
    public void addAtHead(int val) {
        if (size <= 0) {
            head = new Node(val);
            foot = head;
        } else {
            Node temp = head;
            head = new Node(val, null, temp);
            temp.prev = head;
        }
        size++;
    }

    /**
     * Append a node of value val to the last element of the linked list.
     */
    public void addAtTail(int val) {
        if (size <= 0) {
            head = new Node(val);
            foot = head;
        } else {
            Node temp = new Node(val, foot, null);
            foot.next = temp;
            foot = temp;
        }
        size++;
    }

    /**
     * Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted.
     */
    public void addAtIndex(int index, int val) {
        if (index > size) {
            // out of bounds
            return;
        }

        if (index <= 0) {
            addAtHead(val);
            return;
        } else if (index == size) {
            addAtTail(val);
            return;
        }

        int pos = 0;
        Node node = head;
        while (node != null) {
            if (pos == index) {
                Node temp = new Node(val, node.prev, node);
                node.prev.next = temp;
                node.prev = temp;
                break;
            }
            node = node.next;
            pos++;
        }

        size++;
    }

    /**
     * Delete the index-th node in the linked list, if the index is valid.
     */
    public void deleteAtIndex(int index) {
        if (size <= 0) {
            // out of bounds
            return;
        } else if (index >= size) {
            // out of bounds
            return;
        }

        if (index == 0) {
            Node temp = head;
            head = temp.next;
            temp.prev = null;
            temp.next = null;
            if (head != null) head.prev = null;
            size--;
            return;
        } else if (index == size - 1) {
            Node temp = foot;
            foot = temp.prev;
            temp.prev = null;
            temp.next = null;
            if (foot != null) foot.next = null;
            size--;
            return;
        }

        Node node;
        int pos;
        int mid = size / 2;
        if (index > mid) {
            // traversal from foot
            node = foot;
            pos = size - 1;
            while (node != null) {
                if (pos == index) {
                    Node prev = node.prev;
                    Node next = node.next;
                    node.prev = null;
                    node.next = null;
                    prev.next = next;
                    next.prev = prev;
                    break;
                }
                node = node.prev;
                pos--;
            }
        } else {
            // traversal from head
            node = head;
            pos = 0;
            while (node != null) {
                if (pos == index) {
                    Node prev = node.prev;
                    Node next = node.next;
                    node.prev = null;
                    node.next = null;
                    prev.next = next;
                    next.prev = prev;
                    break;
                }
                node = node.next;
                pos++;
            }
        }
        size--;
    }


    private static final class Node {
        int val;
        Node prev;
        Node next;

        public Node(int val) {
            this.val = val;
        }

        public Node(int val, Node prev, Node next) {
            this.val = val;
            this.prev = prev;
            this.next = next;
        }
    }
}
